﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Entity;
using BaseSet.Logic;
using BaseSet.Framework;
using System.Data;
using System.Web.Caching;
using BaseSet.Framework.Config;

namespace WebSite.Admin.UserControl
{
    public partial class Top : System.Web.UI.UserControl
    {       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                 Users user = UserBase.GetCurrentAdminUser();
                 if (user != null)
                 {
                     litName.Text = user.NickName;
                 }

                 FunctionsHelper fHelper = new FunctionsHelper();
                 List<Functions> fc = fHelper.GetCurrPowerFun(user);

                 FunctionItemHelper fiHelper = new FunctionItemHelper();
                 List<FunctionItem> fiList = fiHelper.GetCurrPowerFunItems(user);
                 FunctionItem fstItem = new FunctionItem();
                 List<Functions> topMenus = new List<Functions>();

                 if (fc != null && fc.Count > 0)
                 {                    
                     foreach (Functions fun in fc)
                     {
                         if (fun.ParentID == "0" && topMenus.Contains(fun)== false)
                         {
                             fstItem = fiList.Find(delegate(FunctionItem fi) { return fi.FunctionID == fun.ID && fi.IsDisplay == 1; });

                             if (fstItem != null)
                             {
                                 fun.LinkUrl = fstItem.PageUrl;
                             }
                             else
                             {
                                 foreach (Functions subFun in fc)
                                 {
                                     if (subFun.ParentID == fun.ID)
                                     {
                                         fstItem = fiList.Find(delegate(FunctionItem fi) { return fi.FunctionID == subFun.ID && fi.IsDisplay == 1; });
                                         if (fstItem != null)
                                         {
                                             fun.LinkUrl = fstItem.PageUrl;
                                             break;
                                         }
                                     }
                                 }
                             }

                             topMenus.Add(fun);
                         }
                     }
                 }

                 rptMenu.DataSource = topMenus;
                 rptMenu.DataBind();
            }
        }

        #region Exit
        protected void IbtnExit_Click(object sender, ImageClickEventArgs e)
        {
            Session["CurrentAdminUser"] = null;

            this.Cache.Remove("CurrFunItems");
            this.Cache.Remove("CurrFunctions");
            this.Cache.Remove("CurrPowerItem");


            Response.Redirect(UserBase.GetServerPath() + "/Admin/login.aspx");
        }
        #endregion
    }
}